<?php

namespace app\storeapi\controller\apps\dealer;

use app\storeapi\controller\Controller;
use app\storeapi\model\dealer\Withdraw as WithdrawModel;

/**
 * 分销商提现申请
 * Class Setting
 * @package app\store\controller\apps\dealer
 */
class Withdraw extends Controller
{
    /**
     * 提现记录列表
     * @param int $apply_status
     * @param int $page
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $param = $this->postData();
        $model = new WithdrawModel;
        $list = $model->getList($param);
        return $this->renderSuccess($list);
    }

    /**
     * 提现审核
     * @param $id
     * @return array
     * @throws \think\exception\DbException
     */
    public function submit()
    {
        $param = $this->postData();
        $model = WithdrawModel::detail($param['id']);
        if(!$model){
            return $this->renderError('该申请不存在，请刷新后重试');
        }
        if ($model->submit($param)) {
            return $this->renderSuccess([],'操作成功');
        }
        return $this->renderError($model->getError() ?: '操作失败');
    }

    /**
     * 分销商提现：微信支付企业付款
     * @param $id
     * @return array|bool
     * @throws \app\common\exception\BaseException
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function wechatPay()
    {
        $param = $this->postData();
        $model = WithdrawModel::detail($param['id']);
        if(!$model){
            return $this->renderError('该申请不存在，请刷新后重试');
        }

        if ($model->wechatPay($param)) {
            return $this->renderSuccess('操作成功');
        }
        return $this->renderError($model->getError() ?: '操作失败');
    }

    /**
     * 确认打款
     * @param $id
     * @return array
     * @throws \think\exception\DbException
     */
    public function money()
    {
        $param = $this->postData();
        $model = WithdrawModel::detail($param['id']);
        if(!$model){
            return $this->renderError('该申请不存在，请刷新后重试');
        }

        if ($model->money()) {
            return $this->renderSuccess([],'操作成功');
        }
        return $this->renderError($model->getError() ?: '操作失败');
    }



}